package admin

import (
	"gitee.com/gopher2011/gin"
	"gitee.com/gopher2011/mall/app/dao"
	"gitee.com/gopher2011/mall/app/service"
	"net/http"
	"strconv"
)

// 商品品牌管理 /brand
var PmsBrand = &pmsBrandApi{}

type pmsBrandApi struct{}

// 获取全部品牌列表 /listAll Get 参数:无
func (it *pmsBrandApi) GetList(r *gin.Context) {
	res, err := service.PmsBrand.ListAll()
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "获取全部品牌列表失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "获取全部品牌列表成功!", "data": res})
	}
}

// 添加品牌 /create Post 参数: dao.PmsBrandParam
func (it *pmsBrandApi) Create(r *gin.Context) {
	// 将请求参数解析到 ptr上
	var ptr dao.PmsBrandParam
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.PmsBrand.Create(ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "添加品牌失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "添加品牌成功!", "data": res})
	}
}

// 更新品牌 /update/:id Post 参数: id,dao.PmsBrandParam
func (it *pmsBrandApi) Update(r *gin.Context) {
	id, _ := strconv.Atoi(r.Param("id"))
	var ptr dao.PmsBrandParam
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.PmsBrand.Update(int64(id), ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "更新品牌失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "更新品牌成功!", "data": res})
	}
}

// 删除品牌 /delete/:id Get 参数:id
func (it *pmsBrandApi) Delete(r *gin.Context) {
	id, _ := strconv.Atoi(r.Param("id"))
	res, err := service.PmsBrand.Delete(id)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "删除品牌失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "删除品牌成功!", "data": res})
	}
}

// 根据品牌名称分页获取品牌列表 /list Get 参数:dao.PmsBrandList
func (it *pmsBrandApi) List(r *gin.Context) {
	// 将请求参数解析到 ptr上
	var ptr dao.PmsBrandList
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.PmsBrand.List(ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "根据品牌名称分页获取品牌列表失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "根据品牌名称分页获取品牌列表成功!", "data": res})
	}
}

// 根据编号查询品牌信息 /:id Get 参数:id
func (it *pmsBrandApi) GetItem(r *gin.Context) {
	id, _ := strconv.Atoi(r.Param("id"))
	res, err := service.PmsBrand.GetItem(id)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "根据编号查询品牌信息失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "根据编号查询品牌信息成功!", "data": res})
	}
}

// 批量删除品牌 /delete/batch Post 参数:ids
func (it *pmsBrandApi) DeleteBatch(r *gin.Context) {
	ids := make([]int, 0)
	for _, v := range r.PostFormArray("ids") {
		id, _ := strconv.Atoi(v)
		ids = append(ids, id)
	}
	res, err := service.PmsBrand.DeleteBatch(ids)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "批量删除品牌失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "批量删除品牌成功!", "data": res})
	}
}

// 批量更新显示状态 /update/show_status Post 参数:ids,show_status
func (it *pmsBrandApi) UpdateShowStatus(r *gin.Context) {
	ids := make([]int, 0)
	for _, v := range r.PostFormArray("ids") {
		id, _ := strconv.Atoi(v)
		ids = append(ids, id)
	}
	status, _ := strconv.Atoi(r.PostForm("show_status"))
	res, err := service.PmsBrand.UpdateShowStatus(ids, status)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "失败!", "err": err})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "成功!", "data": res})
	}
}

//批量更新厂家制造商状态 /update/factory_status Post 参数:ids,factory_status
func (it *pmsBrandApi) UpdateFactoryStatus(r *gin.Context) {
	ids := make([]int, 0)
	for _, v := range r.PostFormArray("ids") {
		id, _ := strconv.Atoi(v)
		ids = append(ids, id)
	}
	status, _ := strconv.Atoi(r.PostForm("factory_status"))
	res, err := service.PmsBrand.UpdateFactory(ids, status)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "失败!", "err": err})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "成功!", "data": res})
	}
}
